#import <Foundation/Foundation.h>
#import <sys/socket.h>

#import <CFNetwork/CFNetwork.h>



@class GamePeer,UDPSocket, TCPConnection, ThrottledTCPConnection;

/* This class remains largely unchanged. If you are experiencing problems, it is not likely
that the problem is here. */

@protocol GamePeerDelegate <NSObject>
- (void) gamePeerDidFailConnecting:(GamePeer*)peer;
- (void) gamePeerDidConnect:(GamePeer*)peer;
- (void) gamePeerDidDisconnect:(GamePeer*)peer;
- (void) gamePeerDidReceiveData:(NSData*)data;
@end

/*
This class represents a multiplayer game peer and its information. If you obtainted
a GamePeer instance from a GameServer, then it represents a remote client, otherwise,
it represents a remote server.
*/
@interface GamePeer : NSObject
{
@private
	BOOL					_service,
							_server,
							_local;
	NSString*				_uniqueID;
	NSString*				_name;
	struct sockaddr*		_address;
//	UDPSocket*				_socket;
	ThrottledTCPConnection *_connection;
    BOOL                    _connectionEstablished;
    BOOL                    _connectionComplete;
    CFRunLoopRef            _connectionRunLoop;
    
	id						_plist;
	id						_delegate;
	BOOL					_disconnecting;
}
@property(readonly, getter=isServer) BOOL server;
@property(readonly) NSString* uniqueID;
@property(readonly) NSString* name;
@property(readonly) UInt32 IPv4Address; //The returned address is in host-endian
@property(readonly) NSString* address;
@property(readonly, getter=isLocal) BOOL local; //Yes if on the same subnet as the host

@property(readonly, getter=isConnecting) BOOL connecting;
@property(readonly, getter=isConnected) BOOL connected;
@property(readonly) id infoPlist; //Only valid if connected

@property(assign) id<GamePeerDelegate> delegate;
@property(readonly, getter=isService) BOOL service;

+ (GamePeer*) serverWithAddress:(NSString*)address;                         //For instance: @"games.apple.com:123"
+ (GamePeer*) serverWithIPv4Address:(UInt32)address port:(UInt16)port;      //The "address" is assumed to be in host-endian

- (id) initWithCFNetService:(CFNetServiceRef)netService;
- (id) initWithName:(NSString*)name address:(const struct sockaddr*)address;
- (id) initWithConnection:(TCPConnection*)connection;

- (NSString*) addressWithoutPort;

- (BOOL) connect;
- (BOOL) sendData:(NSData*)data;
- (BOOL) sendData:(NSData *)data throttledToBandwidthLimit:(float)kbs;
- (void) disconnect;

- (NSTimeInterval) measureRoundTripLatency;

@end
